package com.mc.dao.Impl;
import com.mc.bean.MJ;
import com.mc.dao.MJDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 赛事评委关系实体类数据访问实现类
 * @author fyp
 */
public class MJDaoImpl extends BaseDao implements MJDao {

    @Override
    public List<MJ> findByMJCode(int mjcode) {
        List<MJ> list=new ArrayList<>();
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "select * from mj where mjcode=?";
            super.getPst(sql);
            super.pstmt.setInt(1,mjcode);
            //执行预编译指令，得到结果
            rs=pstmt.executeQuery();
            while (rs.next()){
                MJ mj=new MJ(rs.getInt(1),rs.getInt(2),rs.getInt(3));
                //将对象放入集合中
                list.add(mj);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return list;
    }

    @Override
    public List<MJ> findByMid(int mid) {
        List<MJ> list=new ArrayList<>();
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "select * from mj where mid=?";
            super.getPst(sql);
            super.pstmt.setInt(1,mid);
            //执行预编译指令，得到结果
            rs=pstmt.executeQuery();
            while (rs.next()){
                MJ mj=new MJ(rs.getInt(1),rs.getInt(2),rs.getInt(3));
                //将对象放入集合中
                list.add(mj);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return list;
    }

    @Override
    public List<MJ> findByJuryId(int juryid) {
        List<MJ> list=new ArrayList<>();
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "select * from mj where juryid=?";
            super.getPst(sql);
            super.pstmt.setInt(1,juryid);
            //执行预编译指令，得到结果
            rs=pstmt.executeQuery();
            while (rs.next()){
                MJ mj=new MJ(rs.getInt(1),rs.getInt(2),rs.getInt(3));
                //将对象放入集合中
                list.add(mj);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return list;
    }

    @Override
    public int add(MJ mj) {
        int i=0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "insert into mj(mjcode,mid,juryid) values (?,?,?)";
            super.getPst(sql);
            super.pstmt.setInt(1,mj.getMjcode());
            super.pstmt.setInt(2,mj.getMid());
            super.pstmt.setInt(3,mj.getJuryid());
            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }

    @Override
    public int delete(int mjcode) {
        int i=0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql="delete from mj where mjcode=?";
            super.getPst(sql);
            super.pstmt.setInt(1,mjcode);
            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }

    @Override
    public int update(MJ mj) {
        int i=0;
        try {
            //加载驱动
            super.regist();
            //创建连接
            super.connection();
            //创建预编译指令
            String sql = "update mj set mid=?,juryid=? where mjcode=?";
            super.getPst(sql);
            super.pstmt.setInt(1,mj.getMid());
            super.pstmt.setInt(2,mj.getJuryid());
            super.pstmt.setInt(3,mj.getMjcode());
            //执行预编译指令，得到结果
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            super.close();
        }
        return i;
    }
}
